package com.xjcs.saplingplan.dialog

import android.content.Context
import com.lxj.xpopup.core.CenterPopupView
import com.xjcs.saplingplan.R
import com.xjcs.saplingplan.const.setSingleClick
import com.xjcs.saplingplan.const.toToast
import com.xjcs.saplingplan.databinding.PopCountdownCateCreateBinding
import com.xjcs.saplingplan.datastore.AppData
import com.xjcs.saplingplan.room.bean.countdown.PlanCateBean
import com.xjcs.saplingplan.room.utils.PlanCateRoomUtils

class CountdownCateCreateDialog(
    context: Context,
    private val callback: (String) -> Unit
) : CenterPopupView(context) {
    private lateinit var binding: PopCountdownCateCreateBinding

    override fun getImplLayoutId(): Int {
        return R.layout.pop_countdown_cate_create
    }

    override fun onCreate() {
        super.onCreate()
        binding = PopCountdownCateCreateBinding.bind(contentView)
        initListener()
    }

    private fun initListener() {
        binding.tvCancel.setSingleClick {
            dialog.dismiss()
        }
        binding.tvSure.setSingleClick {
            val cateName = binding.etInput.text.toString().trim()
            if (cateName.isEmpty()) {
                "请输入分类名称".toToast()
                return@setSingleClick
            }
            PlanCateRoomUtils.insert(PlanCateBean().apply {
                planCateUserId = AppData.getUserId()
                planCateName = cateName
                planCateSort = 0      //排序序号
            })
            callback.invoke(cateName)
            dialog.dismiss()
        }
    }
}